<?php
/**
 * File holding the SFCheckboxesInput class
 *
 * @file
 * @ingroup SF
 */

/**
 * The SFCheckboxesInput class.
 *
 * @ingroup SFFormInput
 */
class SFCheckboxesInput extends SFMultiEnumInput
{

    public static function getName()
    {
        return 'checkboxes';
    }

    public static function getDefaultPropTypeLists()
    {
        return array(
            'enumeration' => array()
        );
    }

    public static function getOtherPropTypeListsHandled()
    {
        return array();
    }

    public static function getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, $other_args )
    {
        global $sfgTabIndex, $sfgFieldNum, $sfgShowOnSelect;

        $checkboxClass = ($is_mandatory) ? 'mandatoryField' : 'createboxInput';
        $labelClass = 'checkboxLabel';
        if ( array_key_exists( 'class', $other_args ) ) {
            $labelClass .= ' ' . $other_args['class'];
        }
        $input_id = "input_$sfgFieldNum";
        // get list delimiter - default is comma
        if ( array_key_exists( 'delimiter', $other_args ) ) {
            $delimiter = $other_args['delimiter'];
        } else {
            $delimiter = ',';
        }
        $cur_values = SFUtils::getValuesArray( $cur_value, $delimiter );

        if ( ($possible_values = $other_args['possible_values']) == null ) {
            $possible_values = array();
        }
        $text = '';
        foreach ( $possible_values as $key => $possible_value ) {
            $cur_input_name = $input_name . '[' . $key . ']';

            if (
                array_key_exists( 'value_labels', $other_args ) &&
                is_array( $other_args['value_labels'] ) &&
                array_key_exists( $possible_value, $other_args['value_labels'] )
            ) {
                $label = $other_args['value_labels'][$possible_value];
            } else {
                $label = $possible_value;
            }

            $checkbox_attrs = array(
                'id' => $input_id,
                'tabindex' => $sfgTabIndex,
                'class' => $checkboxClass,
            );
            if ( in_array( $possible_value, $cur_values ) ) {
                $checkbox_attrs['checked'] = 'checked';
            }
            if ( $is_disabled ) {
                $checkbox_attrs['disabled'] = 'disabled';
            }
            $checkbox_input = Html::input( $cur_input_name, $possible_value, 'checkbox', $checkbox_attrs );

            // Put a <label> tag around each checkbox, for CSS
            // purposes as well as to clarify this element.
            $text .= "\t" . Html::rawElement( 'label',
                array( 'class' => $labelClass ),
                $checkbox_input . ' ' . $label
            ) . "\n";
            $sfgTabIndex++;
            $sfgFieldNum++;
        }

        $outerSpanID = "span_$sfgFieldNum";
        $outerSpanClass = 'checkboxesSpan';
        if ( $is_mandatory ) {
            $outerSpanClass .= ' mandatoryFieldSpan';
        }

        if ( array_key_exists( 'show on select', $other_args ) ) {
            $outerSpanClass .= ' sfShowIfChecked';
            foreach ( $other_args['show on select'] as $div_id => $options ) {
                if ( array_key_exists( $outerSpanID, $sfgShowOnSelect ) ) {
                    $sfgShowOnSelect[$outerSpanID][] = array( $options, $div_id );
                } else {
                    $sfgShowOnSelect[$outerSpanID] = array( array( $options, $div_id ) );
                }
            }
        }

        $outerSpanAttrs = array( 'id' => $outerSpanID, 'class' => $outerSpanClass );

        //Values from query
        if ( array_key_exists( 'values from query', $other_args ) ) {
            $outerSpanAttrs['vfq'] = $other_args['values from query'];
            $outerSpanAttrs['class'] .= ' ValuesFromQueryTarget';
            $outerSpanAttrs['inputname'] = $input_name;
            //If there some substitution, we pass it to tag parameters
            if ( array_key_exists( 'substitution', $other_args ) ) {
                $outerSpanAttrs['vfqs'] = $other_args['substitution'];
            }
        }

        $text .= Html::hidden( $input_name . '[is_list]', 1 );
        $text = "\t" . Html::rawElement( 'span', $outerSpanAttrs, $text ) . "\n";

        return $text;
    }

    /**
     * Returns the HTML code to be included in the output page for this input.
     */
    public function getHtmlText()
    {
        return self::getHTML(
            $this->mCurrentValue,
            $this->mInputName,
            $this->mIsMandatory,
            $this->mIsDisabled,
            $this->mOtherArgs
        );
    }
}
